# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
import matplotlib.pyplot as pyplot
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import sklearn.metrics as metrics

data = pd.read_csv('../diabetes.csv')

## 各个数据的相关性不高，并且基本全部是 int64 和 float64 ， 直接对 x_tain 数据的标准化 , y 因为是 0，1 不做
X_train = data.drop("Outcome", axis=1)
y_train = data["Outcome"]

ss_X = StandardScaler()
X_train_trans = ss_X.fit_transform(X_train)

# 拆分 20% 数据作为测试集
X_train_part, X_test, y_train_part, y_test = train_test_split(X_train_trans, y_train, train_size=0.8, random_state=0)

## turn back to DataFrame
X_train_part = pd.DataFrame(data=X_train_part, columns=X_train.columns)
X_test_part = pd.DataFrame(data=X_test, columns=X_train.columns)

# logistic回归
from sklearn.linear_model import LogisticRegression
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import cross_val_score

lr = LogisticRegression()
loss = cross_val_score(lr, X_train_part, y=y_train_part, scoring="neg_log_loss", cv=5)
print("LogisticRegression")
print 'logloss of each fold is: ', -loss
print'cv logloss is:', -loss.mean()

penaltys = ["l1", "l2"]
Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
map = dict(penalty=penaltys, C=Cs)
lr_penalty = LogisticRegression()
grid = GridSearchCV(lr_penalty, map, cv=5, scoring='neg_log_loss')
grid.fit(X_train_part, y_train_part)

print("GridSearchCV")
print(-grid.best_score_)
print(grid.best_params_)

y_predict = grid.predict(X_test_part)

print("Classification report for classifier %s:\n%s\n"
      % (grid, metrics.classification_report(y_test, y_predict)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(y_test, y_predict))

